package com.pimwi.dp
{
  import com.pimwi.model.ModelLocator;
  import com.pimwi.vo.Group;
  
  import flash.events.Event;
  
  import mx.collections.ArrayCollection;
  
  [Bindable]
  public class TaskBarDP
  {
    private var _filteredGroup:GroupDP;
    private var _searchExpression:String;

    public function TaskBarDP()
    {
      var group:Group = new Group(-1, 0, "Tous");
      _filteredGroup = new GroupDP(group);
      _searchExpression = "";
    }

    [Bindable("searchExpressionChanged")]
    public function get searchExpression():String
    {
      return _searchExpression;
    }

    [Bindable("searchExpressionChanged")]
    public function get searchInputText():String
    {
      if (_searchExpression == "")
      {
        return " Rechercher...";
      }
      return _searchExpression;
    }

    public function set searchExpression(value:String):void
    {
      _searchExpression = value;
      dispatchEvent(new Event("searchExpressionChanged"));
    }

    [Bindable("filteredGroupChanged")]
    public function get filteredGroup():GroupDP
    {
      return _filteredGroup;
    }

    public function set filteredGroup(value:GroupDP):void
    {
      _filteredGroup = value;
      refreshFilteredContacts();
      dispatchEvent(new Event("filteredGroupChanged"));
    }

    private function refreshFilteredContacts():void
    {
      var locator:ModelLocator = ModelLocator.getInstance();
      var userContacts:ArrayCollection = locator.userContacts;
      var filteredContacts:ArrayCollection = new ArrayCollection();
      for each (var _contact:ContactDP in userContacts)
      {
        if (_filteredGroup.id == -1 || _contact.group.id == _filteredGroup.id)
        {
          filteredContacts.addItem(_contact);
        }
      }
      locator.filteredContacts = filteredContacts;
    }

  }
}